home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / c_tutor.arc / TEXT.ARC / CHAP6.TXT < prev    next >
Text File  |  1990-08-08  |  9KB  |  191 lines

  1.  
  2.                        Chapter 6 - Defines and Macros
  3.  
  4.  
  5.               DEFINES AND MACROS ARE AIDS TO CLEAR PROGRAMMING
  6.  
  7.              Load and display the file named DEFINE.C for your first
  8.         look  at  some defines and macros.  Notice  the  first  four
  9.         lines  2  through 5 of the program, each starting  with  the
  10.         word "#define".  This is the way all defines and macros  are
  11.         defined.  Before the actual compilation starts, the compiler
  12.         goes  through  a  preprocessor pass to resolve  all  of  the
  13.         defines.   In the present case, it will find every place  in
  14.         the  program where the combination "START" is found  and  it
  15.         will  simply  replace  it  with the  0  since  that  is  the
  16.         definition.   The  compiler itself will never see  the  word
  17.         "START",  so as far as the compiler is concerned, the  zeros
  18.         were always there.  Note that if the word "START" appears in
  19.         a  text  string  or  a  comment,  it  will  be  ignored  and
  20.         unchanged.
  21.  
  22.              It should be clear to you by now that putting the  word
  23.         "START"  in your program instead of the numeral 0 is only  a
  24.         convenience  to you and actually acts like a  comment  since
  25.         the  word "START" helps you to understand what the  zero  is
  26.         used for.
  27.  
  28.              In  the  case  of a very small program,  such  as  that
  29.         before  you,  it doesn't really matter what  you  use.   If,
  30.         however,  you  had a 2000 line program before  you  with  27
  31.         references  to  the  "START",  it  would  be  a   completely
  32.         different  matter.   If  you wanted to  change  all  of  the
  33.         "START"s in the program to a new number, it would be  simple
  34.         to change the one #define, but difficult to find and  change
  35.         all   of  the  references  to  it  manually,  and   possibly
  36.         disastrous if you missed one or two of the references.
  37.  
  38.              In  the  same manner,  the preprocessor will  find  all
  39.         occurrences of the word "ENDING" and change them to 9,  then
  40.         the  compiler  will  operate  on the changed  file  with  no
  41.         knowledge that "ENDING" ever existed.
  42.  
  43.              It is a fairly common practice in C programming to  use
  44.         all  capital letters for a symbolic constant such as "START"
  45.         and  "ENDING"  and use all lower case letters  for  variable
  46.         names.  You can use any method you choose since it is mostly
  47.         a matter of personal taste.
  48.  
  49.                            IS THIS REALLY USEFUL?
  50.  
  51.              When  we  get  to the  chapters  discussing  input  and
  52.         output,  we  will need an indicator to tell us when we reach
  53.         the end-of-file of an input file.  Since different compilers
  54.         use different numerical values for this,  although most  use
  55.         either a zero or a minus 1, we will write the program with a
  56.  
  57.  
  58.                                   Page 43
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.                        Chapter 6 - Defines and Macros
  69.  
  70.  
  71.         "define"  to define the EOF used by our particular compiler.
  72.         If at some later date,  we change to a new compiler, it is a
  73.         simple matter to change this one "define" to fix the  entire
  74.         program.  In Turbo C, the EOF is defined in the STDIO.H file
  75.         on  line 44.  You can observe this for yourself  by  listing
  76.         this  file.
  77.  
  78.                               WHAT IS A MACRO?
  79.  
  80.              A macro is nothing more than another define, but  since
  81.         it is capable of at least appearing to perform some  logical
  82.         decisions  or  some math functions, it has  a  unique  name.
  83.         Consider line 4 of the program on your screen for an example
  84.         of  a macro.  In this case, anytime the  preprocessor  finds
  85.         the  word  "MAX"  followed by a  group  in  parentheses,  it
  86.         expects  to find two terms in the parentheses and will do  a
  87.         replacement  of the terms into the second definition.   Thus
  88.         the  first  term  will  replace  every  "A"  in  the  second
  89.         definition and the second term will replace every "B" in the
  90.         second definition.  When line 13 of the program is  reached,
  91.         "index" will be substituted for every "A", and "count"  will
  92.         be  substituted for every "B". But this replaceing will  not
  93.         take place in string literals or comments.  Remembering  the
  94.         cryptic  construct we studied a couple of chapters ago  will
  95.         reveal  that "mx" will receive the maximum value of  "index"
  96.         or "count".
  97.  
  98.              In  like  manner, the "MIN" macro will result  in  "mn"
  99.         receiving  the  minimum value of "index"  or  "count".   The
  100.         results are then printed out.  There are a lot of  seemingly
  101.         extra  parentheses in the macro definition but they are  not
  102.         extra,  they  are  essential.  We  will  discuss  the  extra
  103.         parentheses in our next program.
  104.  
  105.              Compile and run DEFINE.C.
  106.  
  107.                          LETS LOOK AT A WRONG MACRO
  108.  
  109.              Load  the  file named MACRO.C and display  it  on  your
  110.         screen for a better look at a macro and its use.  The second
  111.         line  defines a macro named "WRONG" that appears to get  the
  112.         cube of "A",  and indeed it does in some cases, but it fails
  113.         miserably in others.  The second macro named "CUBE" actually
  114.         does get the cube in all cases.
  115.  
  116.              Consider  the program itself where the CUBE of i+offset
  117.         is  calculated.   If  i is 1,  which it is  the  first  time
  118.         through,  then  we will be looking for the cube of 1+5 =  6,
  119.         which  will result in 216.  When using "CUBE",  we group the
  120.         values like this, (1+5)*(1+5)*(1+5) = 6*6*6 = 216.  However,
  121.         when we use WRONG,  we group them as 1+5*1+5*1+5 = 1+5+5+5 =
  122.  
  123.  
  124.                                   Page 44
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.                        Chapter 6 - Defines and Macros
  135.  
  136.  
  137.         16 which is a wrong answer.   The parentheses are  therefore
  138.         required  to  properly  group the  variables  together.   It
  139.         should  be clear to you that either "CUBE" or "WRONG"  would
  140.         arrive  at  a correct answer for a single  term  replacement
  141.         such as we did in the last program.   The correct values  of
  142.         the  cube  and the square of the numbers are printed out  as
  143.         well as the wrong values for your inspection.
  144.  
  145.              Inspection   of  line  24  will  reveal  that  we   are
  146.         evaluating  "5*(i) + (i)" which is 6 if "i" is one,  and  in
  147.         the  second case "5*((i) + (i))" which is 10 if "i' is  one.
  148.         The parentheses around the entire expression assure that the
  149.         value will be evaluated correctly.
  150.  
  151.                       WHAT IS AN ENUMERATION VARIABLE?
  152.  
  153.              Load  and  display  the program  named  ENUM.C  for  an
  154.         example  of  how to use the "enum" type  variable.   Line  4
  155.         contains the first "enum" type variable named "result" which
  156.         is a variable which can take on any of the values  contained
  157.         within  the parentheses.  Actually the variable "result"  is
  158.         an "int" type variable but can be assigned any of the values
  159.         defined for it.  The names within the parentheses are  "int"
  160.         type  constants and can be used anywhere it is legal to  use
  161.         an "int" type constant.  The constant "win" is assigned  the
  162.         value of 0, "tie" the value 1, "bye" the value 2, etc.
  163.  
  164.              In  use,  the variable "result" is used just  like  any
  165.         "int" type variable would be used and can be seen by its use
  166.         in the program.  The "enum" type of variable is intended  to
  167.         be  used by you, the programmer, as a coding aid  since  you
  168.         can use a constant named "mon" for control structures rather
  169.         that  the meaningless (at least to you) value of 1.   Notice
  170.         that  "days" is assigned the values of days of the  week  in
  171.         the remainder of the program.  If you were to use a "switch"
  172.         statement,  it  would  be much more meaningful  to  use  the
  173.         labels "sun", "mon", etc, rather than the more awkward 0, 1,
  174.         2, etc.  The remainder of the program is simple and will  be
  175.         left to your inspection and understanding.
  176.  
  177.         PROGRAMMING EXERCISE
  178.  
  179.         1.   Write a program to count from 7 to -5 by counting down.
  180.              Use #define statements to define the limits. (Hint, you
  181.              will  need to use a decrementing variable in the  third
  182.              part of the "for" loop control.
  183.  
  184.         2.   Add some print statements to MACRO.C to see the  result
  185.              of the erroneous addition macro.
  186.  
  187.  
  188.  
  189.  
  190.                                   Page 45
  191.